package com.sduept.nwld.dataserver.rest.fault;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.fault.manager.FaultManager;
import com.sduept.cim.ledger.excetpion.ManagementUnitException;
import com.sduept.cim.ledger.model.ManagementUnitDict;
import com.sduept.cim.ledger.services.ManagementUnitService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备故障REST服务
 * @author Ningfw
 * @date 2017-07-10
 *
 */
@RestController
@RequestMapping("/equipmentFault/")
public class EquipmentFaultServices {
	//TODO:ManagementUnitService 替换需要测试
	@Autowired
	private FaultManager bxFaultManager;
	@Autowired
	private ManagementUnitService unitM;
	
	private Map<String, int[]> genDeptMap(int size) {
		Map<String, int[]> deptCountMap = new HashMap<String, int[]>();//名-数量
		List<ManagementUnitDict> allDepts;
		try {
			allDepts = unitM.getAllMaintenanceScopes();
			for (ManagementUnitDict dept : allDepts) {
				deptCountMap.put(dept.getName(), new int[size]);
			}
		} catch (ManagementUnitException e) {

			e.printStackTrace();
		}
		return deptCountMap;
	}
	
	/**
	 * 查询某年度、电压等级下故障发电设备
	 * @param faultYear
	 * @return
	 */

	@GetMapping("/getElectricFaultByYearAndVoltage/{year}/{voltage}")
	public String getElectricFaultByYearAndVoltage(@PathVariable("year") String faultYear,@PathVariable("voltage") String voltage){
		JSONArray jsa = new JSONArray();
		String startTime = faultYear + "-01-01 00:00:00";
		String endTime = faultYear + "-12-31 12:59:59";
		String vol = voltage;
		if("0".equals(voltage)) {
			vol = null;
		}
//		List<String[]> faultList = bxFaultManager.findElectricFaultByYear(startYear, endYear);
		List<String[]> faultList = bxFaultManager.findElectricFaultByYearAndVoltage(startTime, endTime,vol);
		Map<String, String> typeMap = new HashMap<String, String>();//故障类型map
		for(String[] objs : faultList){//拿到所有的非重复故障类型
			String type = objs[2];
			if(StringUtils.isNotEmpty(type)){
				typeMap.put(type, type);
			}
		}
		JSONArray allTypes = new JSONArray();//故障类型数组
		for(String key : typeMap.keySet()){
			allTypes.add(typeMap.get(key));
		}
		Map<String, int[]> deptCountMap = genDeptMap(allTypes.size());//名-数量		
//		Map<String,String> deptMap = unitM.getMaintanceDeptsRelationMap();
		// [rank,num,faultType]
		for(String[] objs : faultList){//根据故障类型存放数据
			String rank = objs[0];
			String nums = objs[1];
			String type = objs[2];
//			String pDept = deptMap.get(rank);
			ManagementUnitDict pDept;
			try {
				pDept = unitM.getManagementUnitByName(rank);
				if(null != pDept) {
					int[] numArr = deptCountMap.get(pDept.getName());
					for(int i=0; i < allTypes.size(); i++){
						if(StringUtils.isNotEmpty(type)){
							if(type.equals(allTypes.get(i))){//数组和类型的顺序一致
								numArr[i] = numArr[i]+Integer.parseInt(nums);
							}
						}
					}
				}
			} catch (ManagementUnitException e) {

				e.printStackTrace();
			}
		}
		JSONObject jsObj = new JSONObject();
		jsObj.put("faultType", allTypes);//故障类型
		jsa.add(jsObj);
		for(String dept : deptCountMap.keySet()){
			JSONObject jso = new JSONObject();
			jso.put("name", dept);
			jso.put("value", deptCountMap.get(dept));
			jsa.add(jso);
		}
		return jsa.toJSONString();
	}
	
}
